<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use common\models\datas\Customer;
use Yii;

/**
 * This is the model class for table "{{%customer_coupons}}".
 *
 * @property integer $id
 * @property integer $coupon_id
 * @property integer $customer_id
 * @property integer $use_status
 * @property integer $created_at
 * @property integer $use_time
 * @property integer $coupons_code_id
 * @property integer $invalid_time
 */
class CustomerCoupons extends \zhtfrontend\components\bases\BaseModel
{
    public $day;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_coupons}}';
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['coupon_id', 'customer_id', 'use_status', 'created_at', 'use_time','coupons_code_id','invalid_time','day'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'coupon_id' => Yii::t('zhtfrontend', '优惠券id'),
            'customer_id' => Yii::t('zhtfrontend', '会员id'),
            'use_status' => Yii::t('zhtfrontend', '使用状态（0 未使用 1已使用）'),
            'created_at' => Yii::t('zhtfrontend', '领取时间'),
            'use_time' => Yii::t('zhtfrontend', '使用时间'),
            'coupons_code_id' => Yii::t('zhtfrontend', '券码id'),
            'invalid_time' => Yii::t('zhtfrontend', '失效时间'),
            'day' => Yii::t('zhtfrontend', '延长天数'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['use_time'] = function () {
            if($this->use_time != 0){
                return \Yii::$app->formatter->asDatetime($this->use_time);
            }else{
                return '--';
            }
        };
        $fields['invalid_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->invalid_time);
        };
        $fields['use_status'] = function (){
            switch ($this->use_status){
                case 0:
                    return \Yii::t('zhtfrontend','未使用');
                case 1;
                    return \Yii::t('zhtfrontend','已使用');
            }
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'customer_name'=> function(){
                return isset($this->customer->name)?$this->customer->name:'';
            },
            'coupons_name'=> function(){
                return isset($this->coupons->name)?$this->coupons->name:'';
            },
            'coupons_code'=> function(){
                return isset($this->couponsCode->code)?$this->couponsCode->code:'';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'delay' => function ($data) {
                            if ($data['use_status'] == 1 ) {
                                return '<button class="yx-btn-disabled" type="button">延迟</button>';
                            } else {
                                return '';
                            }
                        }
                    ]
                )->create();
                //return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function getCoupons(){
        return $this->hasOne(Coupons::className(),['id'=>'coupon_id']);
    }

    public function getCustomer(){
        return $this->hasOne(Customer::className(),['id'=>'customer_id']);
    }

    public function getCouponsCode(){
        return $this->hasOne(CouponsCode::className(),['id'=>'coupons_code_id']);
    }

}
